Passed
Push — master ( 1d44d5...cbfa7c )
by Eric
01:10
created

module.exports   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 51
rs 9.4109

5 Functions

Rating   Name   Duplication   Size   Complexity  
A io.sockets.connection 0 4 1
A io.sockets.connection 0 5 1
A io.sockets.connection 0 4 1
B io.sockets.connection 0 27 1
A io.sockets.connection 0 6 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var io = require('socket.io').listen(5000);
2
var clients = {};
3
4
var verboseServer = false; //Good for debugging the server
5
6
verboseServer && console.log("ChatServer Started"); //If Verbose Debug
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
7
io.sockets.on('connection', function (socket) {
8
  verboseServer && console.log("New Connection"); //If Verbose Debug
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
9
  var userName;
10
  socket.on('connection name',function(user){
11
    verboseServer && console.log("Connection Name"); //If Verbose Debug
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
12
    userName = user.name;
13
    clients[user.name] = socket;
14
    io.sockets.emit('new user', user.name + " has joined.");
15
  });
16
17
18
  socket.on('message', function(msg){
19
    verboseServer && console.log("New msg"); //If Verbose Debug
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
20
    io.sockets.emit('message', msg);
21
  });
22
23
  socket.on('private message', function(msg){
24
    verboseServer && console.log("New PM"); //If Verbose Debug
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
25
    fromMsg = {from:userName, txt:msg.txt}
0 ignored issues
show
Bug introduced by
The variable fromMsg seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.fromMsg.
Loading history...
26
    clients[msg.to].emit('private message', fromMsg);
27
  });
28
29
  socket.on('disconnect', function(){
30
    verboseServer && console.log("disconnect"); //If Verbose Debug
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
31
    delete clients[userName];
32
  });
33
});
34